package jp.co.toshiba.ffsa.action.cslt;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import jp.co.toshiba.ffsa.action.common.CommonAction;
import jp.co.toshiba.ffsa.form.cslt.Cslt101Form;
import jp.co.toshiba.ffsa.service.common.ConstantValue;
import jp.co.toshiba.ffsa.service.cslt.Cslt101Service;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ActionMessagesUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.RequestUtil;

/**
 * 限定公開資料画面
 * @author npc
 */
public class Cslt101Action extends CommonAction{

    /** 画面ID */
    private String screenID = "CSLT101";
    /** ActionForm */
    @ActionForm
    @Resource
    protected Cslt101Form cslt101Form;
    /** Service */
    @Resource
    protected Cslt101Service cslt101Service;
    /** フォームリスト */
    public List<Cslt101Form> cslt101FormList;
    /** カタログ配列リスト */
    public List<String> catalogList;

    /**
     * 初期処理
     * @return 画面遷移先
     * @throws Exception
     */
    @Execute(validator = false)
    public String index() throws Exception {
        // ログイン済判定する
        if(this.judgeLogin(cslt101Form.getSessionId())){
            try{
                // リファラ判定
                if(this.judgeRefferer()){
                    cslt101FormList = new ArrayList<Cslt101Form>();
                    catalogList = new ArrayList<String>();

                    cslt101FormList = cslt101Service.readCsv();
                    catalogList = cslt101Service.calcCatalogByLevel();
                    return "/cslt/cslt101.jsp";
                }else{
                    return this.topTransition;
                }
            }catch(FileNotFoundException e) {
                this.errorLogWriter(e, "index", this.screenID);
                return "/cslt/cslt101.jsp";
            }catch(Exception e){
                this.errorLogWriter(e, "index", this.screenID);
                throw e;
            }
        }else{
            return this.topTransition;
        }
    }

    /**
     * 限定公開資料ダウンロード処理
     * @param file ダウンロードするファイル名
     * @return
     * @throws Exception
     */
    @Execute(validator = false)
    public String download() throws Exception{
        boolean successFlag = false;
        // ログイン済判定する
        if(this.judgeLogin(cslt101Form.getSessionId())){
            // 遷移先
            String transition = null;
            String result = "";
            try {
                // リファラ判定
                if(this.judgeRefferer()){
                    String file = RequestUtil.getRequest().getParameter("fileName");
                    successFlag = cslt101Service.downloadFile(file);
                    if(!successFlag){
                        //エラー表示
                        ActionMessages messages = new ActionMessages();
                        messages.add("downloadFailed", new ActionMessage("errors.download", true));
                        ActionMessagesUtil.addErrors(RequestUtil.getRequest().getSession(true), messages);
                        result = ConstantValue.fail;
                        transition = "/cser/cser101.jsp";
                    }else{
                        result = ConstantValue.success;
                        transition = null;
                    }
                    if(ConstantValue.success.equals(result)){
                        // 操作ログ記述
                        ArrayList<ArrayList<String>> parameters = new ArrayList<ArrayList<String>>();
                        ArrayList<String> parameterNames = new ArrayList<String>();
                        ArrayList<String> values = new ArrayList<String>();
                        parameterNames.add(MessageResourcesUtil.getMessage("cslt101.002"));
                        values.add(file);
                        parameters.add(parameterNames);
                        parameters.add(values);
                        this.entryOperationHistory(this.screenID, "download", parameters, result);
                    }

                    return transition;

                }else{
                    return this.topTransition;
                }
            } catch (Exception e) {
                this.errorLogWriter(e, "download", this.screenID);
                throw e;
            }
        }else{
            return this.topTransition;
        }
    }
}